/********************************************************************
	Copyright (C) 2006-2008 Lensow
	http://www.lensow.com
	Created:	2008/06/14   20:20
	Filename: 	x:\Lensow\Loplayer\src\app\LopSettings.cpp
	Author:		Dean

	Purpose:
*********************************************************************/
#include "StdAfx.h"
#include "LoPlayer.h"
#include "LopSettings.h"
#include "LanguageRes.h"
#include "..\dsutil\DSUtil.h"

CLopSettings::CLopSettings(void)
{
	fInitialized = false;
}

CLopSettings::~CLopSettings(void)
{
}

void CLopSettings::UpdateData(bool bSave)
{
	CWinApp* pApp = AfxGetApp();
	ASSERT(pApp);

	UINT len;
	BYTE* ptr = NULL;

	if (bSave) {
		if (!fInitialized) return;

		pApp->WriteProfileInt(_T("Settings"), _T("HideCaptionMenu"), fHideCaptionMenu);
		pApp->WriteProfileInt(_T("Settings"), _T("ControlState"), nCS);
		pApp->WriteProfileInt(_T("Settings"), _T("DefaultVideoFrame"), iDefaultVideoSize);
		pApp->WriteProfileInt(_T("Settings"), _T("KeepAspectRatio"), fKeepAspectRatio);
		pApp->WriteProfileInt(_T("Settings"), _T("CompMonDeskARDiff"), fCompMonDeskARDiff);
		pApp->WriteProfileInt(_T("Settings"), _T("Volume"), nVolume);
		pApp->WriteProfileInt(_T("Settings"), _T("Balance"), nBalance);
		pApp->WriteProfileInt(_T("Settings"), _T("Mute"), fMute);
		pApp->WriteProfileInt(_T("Settings"), _T("LoopNum"), nLoops);
		pApp->WriteProfileInt(_T("Settings"), _T("Loop"), fLoopForever);
		pApp->WriteProfileInt(_T("Settings"), _T("Rewind"), fRewind);
		pApp->WriteProfileInt(_T("Settings"), _T("Zoom"), iZoomLevel);
		pApp->WriteProfileInt(_T("Settings"), _T("AllowMultipleInstances"), fAllowMultipleInst);
		pApp->WriteProfileInt(_T("Settings"), _T("TitleBarTextStyle"), iTitleBarTextStyle);
		pApp->WriteProfileInt(_T("Settings"), _T("TITLEBARTEXTTITLE"), fTitleBarTextTitle);
		pApp->WriteProfileInt(_T("Settings"), _T("ONTOP"), iOnTop);
		pApp->WriteProfileInt(_T("Settings"), _T("TRAYICON"), fTrayIcon);
		pApp->WriteProfileInt(_T("Settings"), _T("AUTOZOOM"), fRememberZoomLevel);
		pApp->WriteProfileInt(_T("Settings"), _T("FULLSCREENCTRLS"), fShowBarsWhenFullScreen);
		pApp->WriteProfileInt(_T("Settings"), _T("FULLSCREENCTRLSTIMEOUT"), nShowBarsWhenFullScreenTimeOut);
		pApp->WriteProfileBinary(_T("Settings"), _T("FULLSCREENRES"), (BYTE*)&dmFullscreenRes, sizeof(dmFullscreenRes));
		pApp->WriteProfileInt(_T("Settings"), _T("EXITFULLSCREENATTHEEND"), fExitFullScreenAtTheEnd);
		pApp->WriteProfileInt(_T("Settings"), _T("REMEMBERWINDOWPOS"), fRememberWindowPos);
		pApp->WriteProfileInt(_T("Settings"), _T("REMEMBERWINDOWSIZE"), fRememberWindowSize);
		pApp->WriteProfileInt(_T("Settings"), _T("SNAPTODESKTOPEDGES"), fSnapToDesktopEdges);
		pApp->WriteProfileBinary(_T("Settings"), _T("LASTWINDOWRECT"), (BYTE*)&rcLastWindowPos, sizeof(rcLastWindowPos));
		pApp->WriteProfileInt(_T("Settings"), _T("LASTWINDOWTYPE"), lastWindowType);
		pApp->WriteProfileInt(_T("Settings"), _T("ASPECTRATIO_X"), AspectRatio.cx);
		pApp->WriteProfileInt(_T("Settings"), _T("ASPECTRATIO_Y"), AspectRatio.cy);
		pApp->WriteProfileInt(_T("Settings"), _T("KEEPHISTORY"), fKeepHistory);
		pApp->WriteProfileInt(_T("Settings"), _T("DSVIDEORENDERERTYPE"), iDSVideoRendererType);
		pApp->WriteProfileInt(_T("Settings"), _T("RMVIDEORENDERERTYPE"), iRMVideoRendererType);
		pApp->WriteProfileInt(_T("Settings"), _T("QTVIDEORENDERERTYPE"), iQTVideoRendererType);
		pApp->WriteProfileInt(_T("Settings"), _T("APSURACEFUSAGE"), iAPSurfaceUsage);
		pApp->WriteProfileInt(_T("Settings"), _T("VMRSYNCFIX"), fVMRSyncFix);
		pApp->WriteProfileInt(_T("Settings"), _T("DX9_RESIZER"), iDX9Resizer);
		pApp->WriteProfileInt(_T("Settings"), _T("VMR9MIXERMODE"), fVMR9MixerMode);
		pApp->WriteProfileInt(_T("Settings"), _T("VMR9MIXERYUV"), fVMR9MixerYUV);
		pApp->WriteProfileString(_T("Settings"), _T("AUDIORENDERERTYPE"), AudioRendererDisplayName);
		pApp->WriteProfileInt(_T("Settings"), _T("AUTOLOADAUDIO"), fAutoloadAudio);
		pApp->WriteProfileInt(_T("Settings"), _T("AUTOLOADSUBTITLES"), fAutoloadSubtitles);
		pApp->WriteProfileInt(_T("Settings"), _T("ENABLEWORKERTHREADFOROPENING"), fEnableWorkerThreadForOpening);
		pApp->WriteProfileInt(_T("Settings"), _T("REPORTFAILEDPINS"), fReportFailedPins);
		pApp->WriteProfileString(_T("Settings"), _T("DVDPATH"), sDVDPath);
		pApp->WriteProfileInt(_T("Settings"), _T("USEDVDPATH"), fUseDVDPath);
		pApp->WriteProfileInt(_T("Settings"), _T("MENULANG"), idMenuLang);
		pApp->WriteProfileInt(_T("Settings"), _T("AUDIOLANG"), idAudioLang);
		pApp->WriteProfileInt(_T("Settings"), _T("SUBTITLESLANG"), idSubtitlesLang);
		pApp->WriteProfileInt(_T("Settings"), _T("AUTOSPEAKERCONF"), fAutoSpeakerConf);
		CString style;
		pApp->WriteProfileString(_T("Settings"), _T("SPLOGFONT"), style <<= subdefstyle);
		pApp->WriteProfileInt(_T("Settings"), _T("SPOVERRIDEPLACEMENT"), fOverridePlacement);
		pApp->WriteProfileInt(_T("Settings"), _T("SPHORPOS"), nHorPos);
		pApp->WriteProfileInt(_T("Settings"), _T("SPVERPOS"), nVerPos);
		pApp->WriteProfileInt(_T("Settings"), _T("SPCSIZE"), nSPCSize);
		pApp->WriteProfileInt(_T("Settings"), _T("SPCMAXRES"), nSPCMaxRes);
		pApp->WriteProfileInt(_T("Settings"), _T("SUBDELAYINTERVAL"), nSubDelayInterval);
		pApp->WriteProfileInt(_T("Settings"), _T("POW2TEX"), fSPCPow2Tex);
		pApp->WriteProfileInt(_T("Settings"), _T("ENABLESUBTITLES"), fEnableSubtitles);
		pApp->WriteProfileInt(_T("Settings"), _T("ENABLEAUDIOSWITCHER"), fEnableAudioSwitcher);
		pApp->WriteProfileInt(_T("Settings"), _T("ENABLEAUDIOTIMESHIFT"), fAudioTimeShift);
		pApp->WriteProfileInt(_T("Settings"), _T("AUDIOTIMESHIFT"), tAudioTimeShift);
		pApp->WriteProfileInt(_T("Settings"), _T("DOWNSAMPLETO441"), fDownSampleTo441);
		pApp->WriteProfileInt(_T("Settings"), _T("CUSTOMCHANNELMAPPING"), fCustomChannelMapping);
		pApp->WriteProfileBinary(_T("Settings"), _T("SPEAKERTOCHANNELMAPPING"), (BYTE*)pSpeakerToChannelMap, sizeof(pSpeakerToChannelMap));
		pApp->WriteProfileInt(_T("Settings"), _T("AUDIONORMALIZE"), fAudioNormalize);
		pApp->WriteProfileInt(_T("Settings"), _T("AUDIONORMALIZERECOVER"), fAudioNormalizeRecover);
		pApp->WriteProfileInt(_T("Settings"), _T("AUDIOBOOST"), (int)AudioBoost);

		// CASIMIR666 : nouveau settings
		CString		strTemp;
		pApp->WriteProfileInt(_T("Settings"), _T("D3DFULLSCREEN"), fD3DFullscreen);
		pApp->WriteProfileInt(_T("Settings"), _T("MONITOR_AUTOREFRESHRATE"), fMonitorAutoRefreshRate);

		strTemp.Format (_T("%f"), dBrightness);
		pApp->WriteProfileString(_T("Settings"), _T("COLOR_BRIGHTNESS"), strTemp);
		strTemp.Format (_T("%f"), dContrast);
		pApp->WriteProfileString(_T("Settings"), _T("COLOR_CONTRAST"), strTemp);
		strTemp.Format (_T("%f"), dHue);
		pApp->WriteProfileString(_T("Settings"), _T("COLOR_HUE"), strTemp);
		strTemp.Format (_T("%f"), dSaturation);
		pApp->WriteProfileString(_T("Settings"), _T("COLOR_SATURATION"), strTemp);

		pApp->WriteProfileString(_T("Settings"), _T("SHADERLIST"), strShaderList);
		pApp->WriteProfileInt(_T("Settings"), _T("EVR_BUFFERS"), iEvrBuffers);
		pApp->WriteProfileInt(_T("Settings"), _T("SHOWOSD"), (int)fShowOSD);
		pApp->WriteProfileInt(_T("Settings"), _T("LANGUAGE"), (int)iLanguage);


		// Position de lecture des derniers DVD's
		pApp->WriteProfileInt(_T("Settings"), _T("DVDPOS"), (int)fRememberDVDPos);
		pApp->WriteProfileInt(_T("Settings"), _T("FILEPOS"), (int)fRememberFilePos);
		if (fKeepHistory) {
			for (int i = 0; i < MAX_DVD_POSITION; i++) {
				CString		strDVDPos;
				CString		strValue;

				strDVDPos.Format (_T("DVD Position %d"), i);
				strValue = SerializeHex((BYTE*) & DvdPosition[i], sizeof(DVD_POSITION));
				pApp->WriteProfileString(_T("Settings"), strDVDPos, strValue);
			}

			// Position de lecture des derniers fichiers
			for (int i = 0; i < MAX_FILE_POSITION; i++) {
				CString		strFilePos;
				CString		strValue;

				strFilePos.Format (_T("File Name %d"), i);
				pApp->WriteProfileString(_T("Settings"), strFilePos, FilePosition[i].strFile);
				strFilePos.Format (_T("File Position %d"), i);
				strValue.Format (_T("%I64d"), FilePosition[i].llPosition);
				pApp->WriteProfileString(_T("Settings"), strFilePos, strValue);
			}
		}

		pApp->WriteProfileInt(_T("Settings"), _T("LASTFULLSCREEN"), (int)fLastFullScreen);
		// CASIMIR666 : fin nouveaux settings

		{
			for (int i = 0; ; i++) {
				CString key;
				key.Format(_T("%s\\%04d"), _T("Filters"), i);
				int j = pApp->GetProfileInt(key, _T("Enabled"), -1);
				pApp->WriteProfileString(key, NULL, NULL);
				if (j < 0) break;
			}
			pApp->WriteProfileString(_T("Filters"), NULL, NULL);

			POSITION pos = filters.GetHeadPosition();
			for (int i = 0; pos; i++) {
				FilterOverride* f = filters.GetNext(pos);

				if (f->fTemporary)
					continue;

				CString key;
				key.Format(_T("%s\\%04d"), _T("Filters"), i);

				pApp->WriteProfileInt(key, _T("SourceType"), (int)f->type);
				pApp->WriteProfileInt(key, _T("Enabled"), (int)!f->fDisabled);
				if (f->type == FilterOverride::REGISTERED) {
					pApp->WriteProfileString(key, _T("DisplayName"), CString(f->dispname));
					pApp->WriteProfileString(key, _T("Name"), f->name);
				} else if (f->type == FilterOverride::EXTERNAL) {
					pApp->WriteProfileString(key, _T("Path"), f->path);
					pApp->WriteProfileString(key, _T("Name"), f->name);
					pApp->WriteProfileString(key, _T("CLSID"), CStringFromGUID(f->clsid));
				}
				POSITION pos2 = f->backup.GetHeadPosition();
				for (int i = 0; pos2; i++) {
					CString val;
					val.Format(_T("org%04d"), i);
					pApp->WriteProfileString(key, val, CStringFromGUID(f->backup.GetNext(pos2)));
				}
				pos2 = f->guids.GetHeadPosition();
				for (int i = 0; pos2; i++) {
					CString val;
					val.Format(_T("mod%04d"), i);
					pApp->WriteProfileString(key, val, CStringFromGUID(f->guids.GetNext(pos2)));
				}
				pApp->WriteProfileInt(key, _T("LoadType"), f->iLoadType);
				pApp->WriteProfileInt(key, _T("Merit"), f->dwMerit);
			}
		}

		pApp->WriteProfileInt(_T("Settings"), _T("INTREALMEDIA"), fIntRealMedia);
		// pApp->WriteProfileInt(_T("Settings"), _T("REALMEDIARENDERLESS"), fRealMediaRenderless);
		// pApp->WriteProfileInt(_T("Settings"), _T("QUICKTIMERENDERER"), iQuickTimeRenderer);
		pApp->WriteProfileInt(_T("Settings"), _T("REALMEDIAFPS"), *((DWORD*)&RealMediaQuickTimeFPS));

		pApp->WriteProfileString(_T("Settings\\PNSPRESETS"), NULL, NULL);
		for (int i = 0, j = m_pnspresets.GetCount(); i < j; i++) {
			CString str;
			str.Format(_T("Preset%d"), i);
			pApp->WriteProfileString(_T("Settings\\PNSPRESETS"), str, m_pnspresets[i]);
		}

		pApp->WriteProfileString(_T("Commands2"), NULL, NULL);

		pApp->WriteProfileInt(_T("Settings"), _T("WINLIRC"), fWinLirc);
		//		pApp->WriteProfileInt(_T("Settings"), _T("PN31"), fPN31Client);
		pApp->WriteProfileString(_T("Settings"), _T("WINLIRCADDR"), WinLircAddr);
		pApp->WriteProfileInt(_T("Settings"), _T("UICE"), fUIce);
		pApp->WriteProfileString(_T("Settings"), _T("UICEADDR"), UIceAddr);
		pApp->WriteProfileInt(_T("Settings"), _T("GLOBALMEDIA"), fGlobalMedia);

		pApp->WriteProfileInt(_T("Settings"), _T("DISABLEXPTOOLBARS"), fDisabeXPToolbars);
		pApp->WriteProfileInt(_T("Settings"), _T("USEWMASFREADER"), fUseWMASFReader);
		pApp->WriteProfileInt(_T("Settings"), _T("JUMPDISTS"), nJumpDistS);
		pApp->WriteProfileInt(_T("Settings"), _T("JUMPDISTM"), nJumpDistM);
		pApp->WriteProfileInt(_T("Settings"), _T("JUMPDISTL"), nJumpDistL);
		pApp->WriteProfileInt(_T("Settings"), _T("FREEWINDOWRESIZING"), fFreeWindowResizing);
		pApp->WriteProfileInt(_T("Settings"), _T("NOTIFYMSN"), fNotifyMSN);
		pApp->WriteProfileInt(_T("Settings"), _T("NOTIFYGTSDLL"), fNotifyGTSdll);

		pApp->WriteProfileInt(_T("Internal Filters"), _T("SRCFILTERS"), SrcFilters | ~(SRC_LAST - 1));
		pApp->WriteProfileInt(_T("Internal Filters"), _T("TRAFILTERS"), TraFilters | ~(TRA_LAST - 1));

		pApp->WriteProfileString(_T("Settings"), _T("LOGOFILE"), logofn);
		pApp->WriteProfileInt(_T("Settings"), _T("LOGOEXT"), logoext);

		pApp->WriteProfileInt(_T("Settings"), _T("HIDECDROMSSUBMENU"), fHideCDROMsSubMenu);

		pApp->WriteProfileInt(_T("Settings"), _T("PRIORITY"), priority);
		pApp->WriteProfileInt(_T("Settings"), _T("LAUNCHFULLSCREEN"), launchfullscreen);

		pApp->WriteProfileInt(_T("Settings"), _T("ENABLEWEBSERVER"), fEnableWebServer);
		pApp->WriteProfileInt(_T("Settings"), _T("WEBSERVERPORT"), nWebServerPort);
		pApp->WriteProfileInt(_T("Settings"), _T("WEBSERVERPRINTDEBUGINFO"), fWebServerPrintDebugInfo);
		pApp->WriteProfileInt(_T("Settings"), _T("WEBSERVERUSECOMPRESSION"), fWebServerUseCompression);
		pApp->WriteProfileInt(_T("Settings"), _T("WEBSERVERLOCALHOSTONLY"), fWebServerLocalhostOnly);
		pApp->WriteProfileString(_T("Settings"), _T("WEBROOT"), WebRoot);
		pApp->WriteProfileString(_T("Settings"), _T("WEBDEFINDEX"), WebDefIndex);
		pApp->WriteProfileString(_T("Settings"), _T("WEBSERVERCGI"), WebServerCGI);

		pApp->WriteProfileString(_T("Settings"), _T("SNAPSHOTPATH"), SnapShotPath);
		pApp->WriteProfileString(_T("Settings"), _T("SNAPSHOTEXT"), SnapShotExt);

		pApp->WriteProfileInt(_T("Settings"), _T("THUMBROWS"), ThumbRows);
		pApp->WriteProfileInt(_T("Settings"), _T("THUMBCOLS"), ThumbCols);
		pApp->WriteProfileInt(_T("Settings"), _T("THUMBWIDTH"), ThumbWidth);

		pApp->WriteProfileString(_T("Settings"), _T("ISDB"), ISDb);

		pApp->WriteProfileString(_T("Shaders"), NULL, NULL);
		pApp->WriteProfileInt(_T("Shaders"), _T("Initialized"), 1);
		//pApp->WriteProfileString(_T("Shaders"), _T("Combine"), m_shadercombine);

	} else {
		if (fInitialized) return;

		OSVERSIONINFO vi;
		vi.dwOSVersionInfoSize = sizeof(vi);
		GetVersionEx(&vi);
		fXpOrBetter = (vi.dwMajorVersion >= 5 && vi.dwMinorVersion >= 1 || vi.dwMajorVersion >= 6);

		iDXVer = 0;
		CRegKey dxver;
		if (ERROR_SUCCESS == dxver.Open(HKEY_LOCAL_MACHINE, _T("SOFTWARE\\Microsoft\\DirectX"), KEY_READ)) {
			CString str;
			ULONG len = 64;
			if (ERROR_SUCCESS == dxver.QueryStringValue(_T("Version"), str.GetBuffer(len), &len)) {
				str.ReleaseBuffer(len);
				int ver[4];
				_stscanf(str, _T("%d.%d.%d.%d"), ver + 0, ver + 1, ver + 2, ver + 3);
				iDXVer = ver[1];
			}
		}

		// Set interface language first!
		iLanguage  = pApp->GetProfileInt(_T("Settings"), _T("LANGUAGE"), 0);

		fHideCaptionMenu = !!pApp->GetProfileInt(_T("Settings"), _T("HideCaptionMenu"), 0);
		nCS = pApp->GetProfileInt(_T("Settings"), _T("ControlState"), CS_SEEKBAR | CS_TOOLBAR | CS_STATUSBAR);
		iDefaultVideoSize = pApp->GetProfileInt(_T("Settings"), _T("DefaultVideoFrame"), DVS_FROMINSIDE);
		fKeepAspectRatio = !!pApp->GetProfileInt(_T("Settings"), _T("KeepAspectRatio"), TRUE);
		fCompMonDeskARDiff = !!pApp->GetProfileInt(_T("Settings"), _T("CompMonDeskARDiff"), FALSE);
		nVolume = pApp->GetProfileInt(_T("Settings"), _T("Volume"), 100);
		nBalance = pApp->GetProfileInt(_T("Settings"), _T("Balance"), 0);
		fMute = !!pApp->GetProfileInt(_T("Settings"), _T("Mute"), 0);
		nLoops = pApp->GetProfileInt(_T("Settings"), _T("LoopNum"), 1);
		fLoopForever = !!pApp->GetProfileInt(_T("Settings"), _T("Loop"), 0);
		fRewind = !!pApp->GetProfileInt(_T("Settings"), _T("Rewind"), FALSE);
		iZoomLevel = pApp->GetProfileInt(_T("Settings"), _T("Zoom"), 1);
		iDSVideoRendererType = pApp->GetProfileInt(_T("Settings"), _T("DSVIDEORENDERERTYPE"), VIDRNDT_DS_VMR9WINDOWED );
		iRMVideoRendererType = pApp->GetProfileInt(_T("Settings"), _T("RMVIDEORENDERERTYPE"), VIDRNDT_RM_DEFAULT);
		iQTVideoRendererType = pApp->GetProfileInt(_T("Settings"), _T("QTVIDEORENDERERTYPE"), VIDRNDT_QT_DEFAULT);
		iAPSurfaceUsage = pApp->GetProfileInt(_T("Settings"), _T("APSURACEFUSAGE"), VIDRNDT_AP_TEXTURE2D);
		fVMRSyncFix = !!pApp->GetProfileInt(_T("Settings"), _T("VMRSYNCFIX"), FALSE);
		iDX9Resizer = pApp->GetProfileInt(_T("Settings"), _T("DX9_RESIZER"), 1);
		fVMR9MixerMode = !!pApp->GetProfileInt(_T("Settings"), _T("VMR9MIXERMODE"), TRUE);
		fVMR9MixerYUV = !!pApp->GetProfileInt(_T("Settings"), _T("VMR9MIXERYUV"), FALSE);
		AudioRendererDisplayName = pApp->GetProfileString(_T("Settings"), _T("AUDIORENDERERTYPE"), _T(""));
		fAutoloadAudio = !!pApp->GetProfileInt(_T("Settings"), _T("AUTOLOADAUDIO"), TRUE);
		fAutoloadSubtitles = !!pApp->GetProfileInt(_T("Settings"), _T("AUTOLOADSUBTITLES"), !IsVSFilterInstalled() );
		fEnableWorkerThreadForOpening = !!pApp->GetProfileInt(_T("Settings"), _T("ENABLEWORKERTHREADFOROPENING"), TRUE);
		fReportFailedPins = !!pApp->GetProfileInt(_T("Settings"), _T("REPORTFAILEDPINS"), TRUE);
		fAllowMultipleInst = !!pApp->GetProfileInt(_T("Settings"), _T("AllowMultipleInstances"), 0);
		iTitleBarTextStyle = pApp->GetProfileInt(_T("Settings"), _T("TitleBarTextStyle"), 1);
		fTitleBarTextTitle = !!pApp->GetProfileInt(_T("Settings"), _T("TITLEBARTEXTTITLE"), FALSE);
		iOnTop = pApp->GetProfileInt(_T("Settings"), _T("ONTOP"), 0);
		fTrayIcon = !!pApp->GetProfileInt(_T("Settings"), _T("TRAYICON"), 0);
		fRememberZoomLevel = !!pApp->GetProfileInt(_T("Settings"), _T("AUTOZOOM"), 1);
		fShowBarsWhenFullScreen = !!pApp->GetProfileInt(_T("Settings"), _T("FULLSCREENCTRLS"), 1);
		nShowBarsWhenFullScreenTimeOut = pApp->GetProfileInt(_T("Settings"), _T("FULLSCREENCTRLSTIMEOUT"), 0);
		if (pApp->GetProfileBinary(_T("Settings"), _T("FULLSCREENRES"), &ptr, &len)) {
			memcpy(&dmFullscreenRes, ptr, sizeof(dmFullscreenRes));
			delete [] ptr;
		} else {
			dmFullscreenRes.fValid = false;
		}
		fExitFullScreenAtTheEnd = !!pApp->GetProfileInt(_T("Settings"), _T("EXITFULLSCREENATTHEEND"), 1);
		fRememberWindowPos = !!pApp->GetProfileInt(_T("Settings"), _T("REMEMBERWINDOWPOS"), 0);
		fRememberWindowSize = !!pApp->GetProfileInt(_T("Settings"), _T("REMEMBERWINDOWSIZE"), 0);
		fSnapToDesktopEdges = !!pApp->GetProfileInt(_T("Settings"), _T("SNAPTODESKTOPEDGES"), 0);
		AspectRatio.cx = pApp->GetProfileInt(_T("Settings"), _T("ASPECTRATIO_X"), 0);
		AspectRatio.cy = pApp->GetProfileInt(_T("Settings"), _T("ASPECTRATIO_Y"), 0);
		fKeepHistory = !!pApp->GetProfileInt(_T("Settings"), _T("KEEPHISTORY"), 0);
		if (pApp->GetProfileBinary(_T("Settings"), _T("LASTWINDOWRECT"), &ptr, &len)) {
			memcpy(&rcLastWindowPos, ptr, sizeof(rcLastWindowPos));
			delete [] ptr;
		} else {
			fRememberWindowPos = false;
		}
		lastWindowType = pApp->GetProfileInt(_T("Settings"), _T("LASTWINDOWTYPE"), SIZE_RESTORED);
		sDVDPath = pApp->GetProfileString(_T("Settings"), _T("DVDPATH"), _T(""));
		fUseDVDPath = !!pApp->GetProfileInt(_T("Settings"), _T("USEDVDPATH"), 0);
		idMenuLang = pApp->GetProfileInt(_T("Settings"), _T("MENULANG"), ::GetUserDefaultLCID());
		idAudioLang = pApp->GetProfileInt(_T("Settings"), _T("AUDIOLANG"), ::GetUserDefaultLCID());
		idSubtitlesLang = pApp->GetProfileInt(_T("Settings"), _T("SUBTITLESLANG"), ::GetUserDefaultLCID());
		fAutoSpeakerConf = !!pApp->GetProfileInt(_T("Settings"), _T("AUTOSPEAKERCONF"), 1);
		// TODO: rename subdefstyle -> defStyle, "SPLOGFONT -> "SPSTYLE
		subdefstyle <<= pApp->GetProfileString(_T("Settings"), _T("SPLOGFONT"), _T(""));
		fOverridePlacement = !!pApp->GetProfileInt(_T("Settings"), _T("SPOVERRIDEPLACEMENT"), 0);
		nHorPos = pApp->GetProfileInt(_T("Settings"), _T("SPHORPOS"), 50);
		nVerPos = pApp->GetProfileInt(_T("Settings"), _T("SPVERPOS"), 90);
		nSPCSize = pApp->GetProfileInt(_T("Settings"), _T("SPCSIZE"), 3);
		nSPCMaxRes = pApp->GetProfileInt(_T("Settings"), _T("SPCMAXRES"), 2);
		nSubDelayInterval = pApp->GetProfileInt(_T("Settings"), _T("SUBDELAYINTERVAL"), 500);
		fSPCPow2Tex = !!pApp->GetProfileInt(_T("Settings"), _T("POW2TEX"), TRUE);
		fEnableSubtitles = !!pApp->GetProfileInt(_T("Settings"), _T("ENABLESUBTITLES"), TRUE);
		fEnableAudioSwitcher = !!pApp->GetProfileInt(_T("Settings"), _T("ENABLEAUDIOSWITCHER"), FALSE);
		fAudioTimeShift = !!pApp->GetProfileInt(_T("Settings"), _T("ENABLEAUDIOTIMESHIFT"), 0);
		tAudioTimeShift = pApp->GetProfileInt(_T("Settings"), _T("AUDIOTIMESHIFT"), 0);
		fDownSampleTo441 = !!pApp->GetProfileInt(_T("Settings"), _T("DOWNSAMPLETO441"), 0);
		fCustomChannelMapping = !!pApp->GetProfileInt(_T("Settings"), _T("CUSTOMCHANNELMAPPING"), 0);
		if (pApp->GetProfileBinary(_T("Settings"), _T("SPEAKERTOCHANNELMAPPING"), &ptr, &len)) {
			memcpy(pSpeakerToChannelMap, ptr, sizeof(pSpeakerToChannelMap));
			delete [] ptr;
		} else {
			memset(pSpeakerToChannelMap, 0, sizeof(pSpeakerToChannelMap));
			for (int j = 0; j < 18; j++)
				for (int i = 0; i <= j; i++)
					pSpeakerToChannelMap[j][i] = 1 << i;

			pSpeakerToChannelMap[0][0] = 1 << 0;
			pSpeakerToChannelMap[0][1] = 1 << 0;

			pSpeakerToChannelMap[3][0] = 1 << 0;
			pSpeakerToChannelMap[3][1] = 1 << 1;
			pSpeakerToChannelMap[3][2] = 0;
			pSpeakerToChannelMap[3][3] = 0;
			pSpeakerToChannelMap[3][4] = 1 << 2;
			pSpeakerToChannelMap[3][5] = 1 << 3;

			pSpeakerToChannelMap[4][0] = 1 << 0;
			pSpeakerToChannelMap[4][1] = 1 << 1;
			pSpeakerToChannelMap[4][2] = 1 << 2;
			pSpeakerToChannelMap[4][3] = 0;
			pSpeakerToChannelMap[4][4] = 1 << 3;
			pSpeakerToChannelMap[4][5] = 1 << 4;
		}

		fAudioNormalize = !!pApp->GetProfileInt(_T("Settings"), _T("AUDIONORMALIZE"), FALSE);
		fAudioNormalizeRecover = !!pApp->GetProfileInt(_T("Settings"), _T("AUDIONORMALIZERECOVER"), TRUE);
		AudioBoost = pApp->GetProfileInt(_T("Settings"), _T("AUDIOBOOST"), 1);

		{
			for (int i = 0; ; i++) {
				CString key;
				key.Format(_T("%s\\%04d"), _T("Filters"), i);

				CAutoPtr<FilterOverride> f(new FilterOverride);

				f->fDisabled = !pApp->GetProfileInt(key, _T("Enabled"), 0);

				UINT j = pApp->GetProfileInt(key, _T("SourceType"), -1);
				if (j == 0) {
					f->type = FilterOverride::REGISTERED;
					f->dispname = CStringW(pApp->GetProfileString(key, _T("DisplayName"), _T("")));
					f->name = pApp->GetProfileString(key, _T("Name"), _T(""));
				} else if (j == 1) {
					f->type = FilterOverride::EXTERNAL;
					f->path = pApp->GetProfileString(key, _T("Path"), _T(""));
					f->name = pApp->GetProfileString(key, _T("Name"), _T(""));
					f->clsid = GUIDFromCString(pApp->GetProfileString(key, _T("CLSID"), _T("")));
				} else {
					pApp->WriteProfileString(key, NULL, 0);
					break;
				}

				f->backup.RemoveAll();
				for (int i = 0; ; i++) {
					CString val;
					val.Format(_T("org%04d"), i);
					CString guid = pApp->GetProfileString(key, val, _T(""));
					if (guid.IsEmpty()) break;
					f->backup.AddTail(GUIDFromCString(guid));
				}

				f->guids.RemoveAll();
				for (int i = 0; ; i++) {
					CString val;
					val.Format(_T("mod%04d"), i);
					CString guid = pApp->GetProfileString(key, val, _T(""));
					if (guid.IsEmpty()) break;
					f->guids.AddTail(GUIDFromCString(guid));
				}

				f->iLoadType = (int)pApp->GetProfileInt(key, _T("LoadType"), -1);
				if (f->iLoadType < 0) break;

				f->dwMerit = pApp->GetProfileInt(key, _T("Merit"), MERIT_DO_NOT_USE + 1);

				filters.AddTail(f);
			}
		}

		fIntRealMedia = !!pApp->GetProfileInt(_T("Settings"), _T("INTREALMEDIA"), 0);
		//fRealMediaRenderless = !!pApp->GetProfileInt(_T("Settings"), _T("REALMEDIARENDERLESS"), 0);
		//iQuickTimeRenderer = pApp->GetProfileInt(_T("Settings"), _T("QUICKTIMERENDERER"), 2);
		RealMediaQuickTimeFPS = 25.0;
		*((DWORD*)&RealMediaQuickTimeFPS) = pApp->GetProfileInt(_T("Settings"), _T("REALMEDIAFPS"), *((DWORD*) & RealMediaQuickTimeFPS));

		m_pnspresets.RemoveAll();
		for (int i = 0; ; i++) {
			CString str;
			str.Format(_T("Preset%d"), i);
			str = pApp->GetProfileString(_T("Settings\\PNSPRESETS"), str, _T(""));
			if (str.IsEmpty()) break;
			m_pnspresets.Add(str);
		}
		if (m_pnspresets.IsEmpty()) {
			double _4p3 = 4.0 / 3.0;
			double _16p9 = 16.0 / 9.0;
			double _185p1 = 1.85 / 1.0;
			double _235p1 = 2.35 / 1.0;

			CString str;
			str.Format(_T("Scale to 16:9 TV,%.3f,%.3f,%.3f,%.3f"), 0.5, 0.5, _4p3 / _4p3, _16p9 / _4p3);
			m_pnspresets.Add(str);
			str.Format(_T("Zoom To Widescreen,%.3f,%.3f,%.3f,%.3f"), 0.5, 0.5, _16p9 / _4p3, _16p9 / _4p3);
			m_pnspresets.Add(str);
			str.Format(_T("Zoom To Ultra-Widescreen,%.3f,%.3f,%.3f,%.3f"), 0.5, 0.5, _235p1 / _4p3, _235p1 / _4p3);
			m_pnspresets.Add(str);
		}


		//CAtlArray<ACCEL> pAccel;
		//pAccel.SetCount(wmcmds.GetCount());
		//POSITION pos = wmcmds.GetHeadPosition();
		//for(int i = 0; pos; i++) pAccel[i] = wmcmds.GetNext(pos);
		//hAccel = CreateAcceleratorTable(pAccel.GetData(), pAccel.GetCount());

		WinLircAddr = pApp->GetProfileString(_T("Settings"), _T("WINLIRCADDR"), _T("127.0.0.1:8765"));
		fWinLirc = !!pApp->GetProfileInt(_T("Settings"), _T("WINLIRC"), 0);
		UIceAddr = pApp->GetProfileString(_T("Settings"), _T("UICEADDR"), _T("127.0.0.1:1234"));
		fUIce = !!pApp->GetProfileInt(_T("Settings"), _T("UICE"), 0);
		//		fPN31Client = !!pApp->GetProfileInt(_T("Settings"), _T("PN31"), 0);
		fGlobalMedia = !!pApp->GetProfileInt(_T("Settings"), _T("GLOBALMEDIA"), 0);

		fDisabeXPToolbars = !!pApp->GetProfileInt(_T("Settings"), _T("DISABLEXPTOOLBARS"), 0);
		fUseWMASFReader = !!pApp->GetProfileInt(_T("Settings"), _T("USEWMASFREADER"), FALSE);
		nJumpDistS = pApp->GetProfileInt(_T("Settings"), _T("JUMPDISTS"), 1000);
		nJumpDistM = pApp->GetProfileInt(_T("Settings"), _T("JUMPDISTM"), 5000);
		nJumpDistL = pApp->GetProfileInt(_T("Settings"), _T("JUMPDISTL"), 20000);
		fFreeWindowResizing = !!pApp->GetProfileInt(_T("Settings"), _T("FREEWINDOWRESIZING"), TRUE);
		fNotifyMSN = !!pApp->GetProfileInt(_T("Settings"), _T("NOTIFYMSN"), FALSE);
		fNotifyGTSdll = !!pApp->GetProfileInt(_T("Settings"), _T("NOTIFYGTSDLL"), FALSE);

		SrcFilters = pApp->GetProfileInt(_T("Internal Filters"), _T("SRCFILTERS"), ~0);
		TraFilters = pApp->GetProfileInt(_T("Internal Filters"), _T("TRAFILTERS"), ~0 ^ TRA_MPEG1);

		logofn = pApp->GetProfileString(_T("Settings"), _T("LOGOFILE"), _T(""));
		logoext = !!pApp->GetProfileInt(_T("Settings"), _T("LOGOEXT"), 0);

		fHideCDROMsSubMenu = !!pApp->GetProfileInt(_T("Settings"), _T("HIDECDROMSSUBMENU"), 0);

		priority = pApp->GetProfileInt(_T("Settings"), _T("PRIORITY"), NORMAL_PRIORITY_CLASS);
		::SetPriorityClass(::GetCurrentProcess(), priority);
		launchfullscreen = !!pApp->GetProfileInt(_T("Settings"), _T("LAUNCHFULLSCREEN"), FALSE);

		fEnableWebServer = !!pApp->GetProfileInt(_T("Settings"), _T("ENABLEWEBSERVER"), FALSE);
		nWebServerPort = pApp->GetProfileInt(_T("Settings"), _T("WEBSERVERPORT"), 13579);
		fWebServerPrintDebugInfo = !!pApp->GetProfileInt(_T("Settings"), _T("WEBSERVERPRINTDEBUGINFO"), FALSE);
		fWebServerUseCompression = !!pApp->GetProfileInt(_T("Settings"), _T("WEBSERVERUSECOMPRESSION"), TRUE);
		fWebServerLocalhostOnly = !!pApp->GetProfileInt(_T("Settings"), _T("WEBSERVERLOCALHOSTONLY"), TRUE);
		WebRoot = pApp->GetProfileString(_T("Settings"), _T("WEBROOT"), _T("*./webroot"));
		WebDefIndex = pApp->GetProfileString(_T("Settings"), _T("WEBDEFINDEX"), _T("index.html;index.php"));
		WebServerCGI = pApp->GetProfileString(_T("Settings"), _T("WEBSERVERCGI"), _T(""));

		CString MyPictures;

		CRegKey key;
		// grrrrr
		// if(!SHGetSpecialFolderPath(NULL, MyPictures.GetBufferSetLength(MAX_PATH), CSIDL_MYPICTURES, TRUE)) MyPictures.Empty();
		// else MyPictures.ReleaseBuffer();
		if (ERROR_SUCCESS == key.Open(HKEY_CURRENT_USER, _T("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders"), KEY_READ)) {
			ULONG len = MAX_PATH;
			if (ERROR_SUCCESS == key.QueryStringValue(_T("My Pictures"), MyPictures.GetBuffer(MAX_PATH), &len)) MyPictures.ReleaseBufferSetLength(len);
			else MyPictures.Empty();
		}
		SnapShotPath = pApp->GetProfileString(_T("Settings"), _T("SNAPSHOTPATH"), MyPictures);
		SnapShotExt = pApp->GetProfileString(_T("Settings"), _T("SNAPSHOTEXT"), _T(".jpg"));

		ThumbRows = pApp->GetProfileInt(_T("Settings"), _T("THUMBROWS"), 4);
		ThumbCols = pApp->GetProfileInt(_T("Settings"), _T("THUMBCOLS"), 4);
		ThumbWidth = pApp->GetProfileInt(_T("Settings"), _T("THUMBWIDTH"), 1024);

		ISDb = pApp->GetProfileString(_T("Settings"), _T("ISDB"), _T("www.opensubtitles.org/isdb"));

		pApp->WriteProfileInt(_T("Settings"), _T("LastUsedPage"), 0);

		// CASIMIR666 : nouveaux settings
		fD3DFullscreen			= !!pApp->GetProfileInt(_T("Settings"), _T("D3DFULLSCREEN"), FALSE);
		fMonitorAutoRefreshRate	= !!pApp->GetProfileInt(_T("Settings"), _T("MONITOR_AUTOREFRESHRATE"), FALSE);

		dBrightness		= _tstof(pApp->GetProfileString(_T("Settings"), _T("COLOR_BRIGHTNESS"),	_T("1")));
		dContrast		= _tstof(pApp->GetProfileString(_T("Settings"), _T("COLOR_CONTRAST"), _T("1")));
		dHue			= _tstof(pApp->GetProfileString(_T("Settings"), _T("COLOR_HUE"), _T("0")));
		dSaturation		= _tstof(pApp->GetProfileString(_T("Settings"), _T("COLOR_SATURATION"),	_T("1")));
		strShaderList	= pApp->GetProfileString(_T("Settings"), _T("SHADERLIST"), _T(""));
		iEvrBuffers		= pApp->GetProfileInt(_T("Settings"), _T("EVR_BUFFERS"), 5);
		fShowOSD		= !!pApp->GetProfileInt(_T("Settings"), _T("SHOWOSD"), 1);

		// Position de lecture des derniers DVD's
		fRememberDVDPos		= !!pApp->GetProfileInt(_T("Settings"), _T("DVDPOS"), 0);
		nCurrentDvdPosition = -1;
		memset (DvdPosition, 0, sizeof(DvdPosition));
		for (int i = 0; i < MAX_DVD_POSITION; i++) {
			CString		strDVDPos;
			CString		strValue;

			strDVDPos.Format (_T("DVD Position %d"), i);
			strValue = pApp->GetProfileString(_T("Settings"), strDVDPos, _T(""));
			if (strValue.GetLength() / 2 == sizeof(DVD_POSITION)) {
				DeserializeHex(strValue, (BYTE*)&DvdPosition[i], sizeof(DVD_POSITION));
			}
		}

		// Position de lecture des derniers fichiers
		fRememberFilePos		= !!pApp->GetProfileInt(_T("Settings"), _T("FILEPOS"), 0);
		nCurrentFilePosition = -1;
		for (int i = 0; i < MAX_FILE_POSITION; i++) {
			CString		strFilePos;
			CString		strValue;

			strFilePos.Format (_T("File Name %d"), i);
			FilePosition[i].strFile = pApp->GetProfileString(_T("Settings"), strFilePos, _T(""));

			strFilePos.Format (_T("File Position %d"), i);
			strValue = pApp->GetProfileString(_T("Settings"), strFilePos, _T(""));
			FilePosition[i].llPosition = _tstoi64 (strValue);
		}

		fLastFullScreen		= !!pApp->GetProfileInt(_T("Settings"), _T("LASTFULLSCREEN"), 0);

		// CASIMIR666 : fin nouveaux settings


		// TODO: sort shaders by label

		//m_shadercombine = pApp->GetProfileString(_T("Shaders"), _T("Combine"), _T(""));

		fInitialized = true;
	}

	m_MediaFormats.UpdateData(bSave);

}

bool CLopSettings::IsVSFilterInstalled()
{
	bool result = false;
	CRegKey key;
	if (ERROR_SUCCESS == key.Open(HKEY_CLASSES_ROOT, _T("CLSID\\{083863F1-70DE-11d0-BD40-00A0C911CE86}\\Instance\\{9852A670-F845-491B-9BE6-EBD841B8A613}"), KEY_READ)) {
		result = true;
	}

	return result;
}
